<script src="js/jquery-1.11.0.js"></script>
<script src="js/register.js"></script>

<?php 
session_start();
if(isset($_SESSION['uid']))
	header('location:account.php');
if(isset($_POST['submit'])){
	$empty = 'You can\'t leave this empty';
	$errorMessage = array('username'=>'','password'=>'','confirmPassword'=>'','securityQuestion1'=>'','securityAnswer1'=>'','securityQuestion2'=>'','securityAnswer1'=>'');
	$allValid = true;
	require('conn.php');
	
	foreach($_POST as $key => $value){
		if($value == ''){
			$allValid = false;
			$errorMessage[$key] = $empty;
		}
	}
	
	//ob_start();
	//require_once('check_username.php');
	//$usernameValid = ob_get_clean();
	//if($usernameValid == 'false'){
        require ('dataBase.php');
	$input = new dataBase;
        $username = $_POST['username'];
        if(!$input->checkName($username)){
                $allValid = false;
		$errorMessage['username'] = 'Someone already has that username. Try another?';
	}
	
	if(!$allValid){
		$_SESSION['errorMessage'] = $errorMessage;
		$_SESSION['previous'] = $_POST;
		
	}
	else{
		$password = $_POST['password'];
		$securityQuestion1 = $_POST['securityQuestion1'];
		$securityAnswer1 = $_POST['securityAnswer1'];
		$securityQuestion2 = $_POST['securityQuestion2'];
		$securityAnswer2 = $_POST['securityAnswer2'];
		$securityQuestion1 = str_replace('\'','\\\'',$securityQuestion1);
		$securityAnswer1 = str_replace('\'','\\\'',$securityAnswer1);
		$securityQuestion2 = str_replace('\'','\\\'',$securityQuestion2);
		$securityAnswer2 = str_replace('\'','\\\'',$securityAnswer2);
		mysql_query("insert into account(uid,username,password,securityQuestion1,securityAnswer1,securityQuestion2,securityAnswer2) values('','$username','$password','$securityQuestion1','$securityAnswer1','$securityQuestion2','$securityAnswer2')");
		header('location:index.php');
	}
}
include_once("header.php");

$previousValue = array('username'=>'','password'=>'','confirmPassword'=>'','securityQuestion1'=>'','securityAnswer1'=>'','securityQuestion2'=>'','securityAnswer2'=>'');
$errorMessage = array('username'=>'','password'=>'','confirmPassword'=>'','securityQuestion1'=>'','securityAnswer1'=>'','securityQuestion2'=>'','securityAnswer2'=>'');

if(isset($_SESSION['previous'])){
	$previousValue = $_SESSION['previous'];
	foreach($_SESSION['errorMessage'] as $key => $value){
		$errorMessage[$key] = '<font style="color:red">'.$value.'</font>';
	}
}
unset($_SESSION['errorMessage']);
unset($_SESSION['previous']);
?>
<form action="" method="post">
	<div id="errorUsername"><?php echo $errorMessage['username']?></div>
	Choose your username<br>
	<input type="text" id="username" name="username" size="30" value="<?php echo $previousValue['username']?>"> <br>
	<br>
	<div id="errorPassword"><?php echo $errorMessage['password']?></div>
	Create a password<br>
	<input type="password" id="password" name="password" size="30" value="<?php echo $previousValue['password']?>"> <br>
	<br>
	<div id="errorPassword2"><?php echo $errorMessage['confirmPassword']?></div>
	Confirm a password<br>
	<input type="password" id="confirmPassword" name="confirmPassword" size="30" value="<?php echo $previousValue['confirmPassword']?>"> <br>
	<br>
	<div id="errorSQ1"><?php echo $errorMessage['securityQuestion1']?></div>
	Create a first security question<br> <input type="text" id="securityQuestion1" name="securityQuestion1" placeholder="Enter your question" size="50" value="<?php echo $previousValue['securityQuestion1']?>">
	<br>
	<br> 
	<div id="errorSA1"><?php echo $errorMessage['securityAnswer1']?></div>
	<input type="text" id="securityAnswer1" name="securityAnswer1" placeholder="Enter your password" size="50" value="<?php echo $previousValue['securityAnswer1']?>"> <br>
	<br>
	<div id="errorSQ2"><?php echo $errorMessage['securityQuestion2']?></div>
	Create a second security question<br> <input type="text" id="securityQuestion2" name="securityQuestion2" placeholder="Enter your question" size="50" value="<?php echo $previousValue['securityQuestion2']?>">
	<br>
	<br> 
	<div id="errorSA2"><?php echo $errorMessage['securityAnswer2']?></div>
	<input type="text" id="securityAnswer2" name="securityAnswer2" placeholder="Enter your password" size="50" value="<?php echo $previousValue['securityAnswer2']?>"> <br>
	<br>
	<input type="checkbox" id="agree" name="agree">I agree the Terms of Service
	<br>
	<br> <input type="submit" name="submit" disabled="disabled" value="Create">
</form>
